﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI.WebControls;
using System.Web;
using System.Web.UI;

namespace publico
{

    /// <summary>
    /// Summary description for clsPaginacao
    /// </summary>

    public static class PUB_PAGINACAO 
    {
 

        private const string _ASCENDENTE = " ASC";

        public static string ASCENDENTE
        {
            get { return _ASCENDENTE; }
        }


        private const string _DESCENDENTE = " DESC";

        public  static string DESCENDENTE
        {
            get { return _DESCENDENTE; }
        }


        public static int PageSize
        {
            get
            {
                if (HttpContext.Current.Session["PageSize"] == null)
                    HttpContext.Current.Session["PageSize"] = 10;
                return (int)HttpContext.Current.Session["PageSize"];
            }
            set
            {
                HttpContext.Current.Session["PageSize"] = value;
            }
        }

        public static int PageIndex
        {
            get
            {
                if (HttpContext.Current.Session["PageIndex"] == null)
                    HttpContext.Current.Session["PageIndex"] = 0;
                return (int)HttpContext.Current.Session["PageIndex"];
            }
            set
            {
                HttpContext.Current.Session["PageIndex"] = value;
            }

        }

        public static SortDirection SortDirection
        {
            get
            {
                if (HttpContext.Current.Session["SortDirection"] == null)
                    HttpContext.Current.Session["SortDirection"] = SortDirection.Ascending;

                return (SortDirection)HttpContext.Current.Session["SortDirection"];
            }
            set
            {
                HttpContext.Current.Session["SortDirection"] = value;
            }
        }


        public static string Sort
        {
            get
            {
                if (HttpContext.Current.Session["Sort"] == null)
                    HttpContext.Current.Session["Sort"] = "";

                return (string)HttpContext.Current.Session["Sort"];

            }
            set
            {
                HttpContext.Current.Session["Sort"] = value;
            }
        }

        public static int TotalDePaginas
        {
            get
            {
                if (HttpContext.Current.Session["TotalDePaginas"] == null)
                    HttpContext.Current.Session["TotalDePaginas"] = 0;
                return (int)HttpContext.Current.Session["TotalDePaginas"];
            }
            set { HttpContext.Current.Session["TotalDePaginas"] = value; }

        }

        public static int TotalDeRegistros
        {
            get
            {
                if (HttpContext.Current.Session["TotalDeRegistros"] == null)
                    HttpContext.Current.Session["TotalDeRegistros"] = 0;
                return (int)HttpContext.Current.Session["TotalDeRegistros"];
            }
            set { HttpContext.Current.Session["TotalDeRegistros"] = value; }

        }

        public static int CalcularTotalDePaginas(Decimal Registros)
        {
            Int32 total;
            total = Convert.ToInt32(Math.Ceiling(Registros / PageSize));
            TotalDePaginas = total;
            TotalDeRegistros = Convert.ToInt32(Registros);
            return total;
        }

        public static void Inicializa()
        {                        
            PUB_PAGINACAO.Sort = string.Empty;
            PUB_PAGINACAO.PageIndex = 0;
            PUB_PAGINACAO.PageSize = 10;
            PUB_PAGINACAO.SortDirection = System.Web.UI.WebControls.SortDirection.Ascending;

        }

        public static void Paginacao(bool MasterPage)
        {
            
            Page currentPage = new Page();
            currentPage = (System.Web.UI.Page)System.Web.HttpContext.Current.Handler;

            ImageButton imgBtnPrimeiro = null;
            ImageButton imgBtnAnterior = null;
            ImageButton imgBtnProximo = null;
            ImageButton imgBtnUltimo = null;

            Label lblPaginaAtual = null;
            Label lblPaginaTotal = null;

            imgBtnPrimeiro = (ImageButton)PUB_FUNCOES.EncontrarControle(currentPage, "IMG_BTN_PRIMEIRO");

            imgBtnAnterior = (ImageButton)PUB_FUNCOES.EncontrarControle(currentPage, "IMG_BTN_ANTERIOR");
            imgBtnProximo = (ImageButton)PUB_FUNCOES.EncontrarControle(currentPage, "IMG_BTN_PROXIMO");
            imgBtnUltimo = (ImageButton)PUB_FUNCOES.EncontrarControle(currentPage, "IMG_BTN_ULTIMO");

            lblPaginaAtual = (Label)PUB_FUNCOES.EncontrarControle(currentPage, "LBL_PAGINA_ATUAL");
            lblPaginaTotal = (Label)PUB_FUNCOES.EncontrarControle(currentPage, "LBL_PAGINA_TOTAL");
            
            lblPaginaAtual.Text = (PUB_PAGINACAO.PageIndex + 1).ToString();
            lblPaginaTotal.Text = PUB_PAGINACAO.TotalDePaginas.ToString();
            if (imgBtnAnterior != null && imgBtnPrimeiro != null && imgBtnProximo != null && imgBtnUltimo != null)
            {

                if (Convert.ToInt32(lblPaginaTotal.Text) > 1)
                {

                    if (PUB_PAGINACAO.PageIndex == 0)
                    {
                        imgBtnPrimeiro.Visible = false;
                        imgBtnAnterior.Visible = false;
                        imgBtnProximo.Visible = true;
                        imgBtnUltimo.Visible = true;
                    }
                    if (PUB_PAGINACAO.PageIndex > 0 && PUB_PAGINACAO.PageIndex < PUB_PAGINACAO.TotalDePaginas - 1)
                    {
                        imgBtnPrimeiro.Visible = true;
                        imgBtnAnterior.Visible = true;
                        imgBtnProximo.Visible = true;
                        imgBtnUltimo.Visible = true;

                    }

                    if (PUB_PAGINACAO.PageIndex == PUB_PAGINACAO.TotalDePaginas - 1)
                    {
                        imgBtnPrimeiro.Visible = true;
                        imgBtnAnterior.Visible = true;
                        imgBtnProximo.Visible = false;
                        imgBtnUltimo.Visible = false;

                    }

                    if (PUB_PAGINACAO.TotalDeRegistros < PUB_PAGINACAO.PageSize)
                    {
                        imgBtnPrimeiro.Visible = false;
                        imgBtnAnterior.Visible = false;
                        imgBtnProximo.Visible = false;
                        imgBtnUltimo.Visible = false;

                    }

                }
                else
                {
                    imgBtnPrimeiro.Visible = false;
                    imgBtnAnterior.Visible = false;
                    imgBtnProximo.Visible = false;
                    imgBtnUltimo.Visible = false;
                    lblPaginaTotal.Text = PUB_PAGINACAO.TotalDePaginas.ToString();
                }
          

        }






        }


    }
}